package lyt.demo4.demos.web.controller;

import lyt.demo4.demos.web.entity.Sys_role;
import lyt.demo4.demos.web.service.Sys_roleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
public class Sys_Controller {

    @Autowired
    private Sys_roleService sys_roleService;

    @PostMapping
    public ResponseEntity<Void> addRole(@RequestBody Sys_role role) {
        sys_roleService.addRole(role);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateRole(@PathVariable Long id, @RequestBody Sys_role role) {
        role.setId(id);
        sys_roleService.updateRole(role);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRole(@PathVariable Long id) {
        sys_roleService.deleteRole(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Sys_role> getRole(@PathVariable Long id) {
        Sys_role role = sys_roleService.getRole(id);
        return ResponseEntity.ok(role);
    }

    @GetMapping
    public ResponseEntity<List<Sys_role>> getAllRoles() {
        List<Sys_role> roles = sys_roleService.getAllRoles();
        return ResponseEntity.ok(roles);
    }
}
